package com.innovation.chengzhangriji.ui.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.StateListDrawable;
import android.support.v7.widget.RecyclerView;
import android.util.StateSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.ui.widget.CuteImageView;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.ImageLoader;

public class DiaryAdapter extends RecyclerView.Adapter<DiaryViewHolder> {

    public DiaryAdapter(DiaryAdapterAgent agent) {
        this.agent = agent;
    }


    public static interface DiaryAdapterAgent extends OnItemClickListener {
        /**
         * 宝宝头像
         */
        public String getBabyHeadUrl(int position);

        /**
         * 日记本名称
         */
        public String getDiaryName(int position);

        /**
         *  宝宝大小
         */
        public String getBabyDay(int position);

        /**
         * 宝宝性别
         */
        public int getBabySex(int position);

        /**
         * 是否属于我
         */
        public boolean canWrite(int position);

        /**
         * 修改日记本名称
         */
        // public void alterDiaryName(int position, String name);

        /**
         * 日记本的数量
         */
        public int getDiaryCount();

        /**
         * 项的点击事件
         */
        @Override
        public void OnItemClick(int position);
    }

    private DiaryAdapterAgent agent;

    @Override
    public DiaryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DiaryViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_diary, null), agent);
    }

    @Override
    public void onBindViewHolder(DiaryViewHolder holder, int position) {
//        holder.imageSex.setVisibility(View.VISIBLE);
//        if(agent.getBabySex(position) == 1) {
//            holder.imageSex.setImageResource(R.mipmap.icon_nan);
//        } else if(agent.getBabySex(position) == 2) {
//            holder.imageSex.setImageResource(R.mipmap.icon_nv);
//        } else {
//            holder.imageSex.setVisibility(View.GONE);
//        }
//        ImageLoader.getInstance().displayImage(agent.getBabyHeadUrl(position), holder.roundedHeadView, Config.getOptionsHeadUnKnown());
        holder.roundedHeadView.setAvatar(agent.getBabyHeadUrl(position));
        holder.roundedHeadView.setSex(agent.getBabySex(position));

        holder.txtDiaryAge.setText(agent.getBabyDay(position));
        holder.txtDiaryName.setText(agent.getDiaryName(position));
        if(agent.canWrite(position)) {
            holder.imageEdit.setVisibility(View.VISIBLE);
        } else {
            holder.imageEdit.setVisibility(View.GONE);
        }
        if(position == getItemCount() - 1) {
            holder.lineView.setVisibility(View.INVISIBLE);
//            ((RelativeLayout.LayoutParams)holder.lineView.getLayoutParams()).addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        } else {
            holder.lineView.setVisibility(View.VISIBLE);
//            ((RelativeLayout.LayoutParams)holder.lineView.getLayoutParams()).addRule(RelativeLayout.ALIGN_LEFT, R.id.text_diary_name);
        }
    }

    @Override
    public int getItemCount() {
        return agent.getDiaryCount();
    }

}
